package com.wangzhen.spring.bean.definition;

import com.wangzhen.spring.bean.factory.DefaultUserFactory;
import com.wangzhen.spring.bean.factory.UserFactory;
import com.wangzhen.spring.ioc.dependcy.entity.User;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * Spring Bean 初始化演示
 * 1、@PostConstruct
 * 2、实现InitializingBean接口afterProperties方法
 * 3、通过Bean注解上的initMethod方法
 * @author dell
 * @since 2020/10/22 12:45
 */
public class BeanInitializationDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext factory = new AnnotationConfigApplicationContext();
        // 注册配置类
//        factory.register(AnnotationBeanDefinitionDemo.class);
        factory.register(BeanInitializationDemo.class);
        // 虽然方法名称是refresh，其实相当于启动容器的操作
        factory.refresh();
        // 获取bean
        UserFactory userFactory = factory.getBean(UserFactory.class);
        System.out.println(userFactory);

        factory.close();
    }

    @Bean(initMethod = "initMethod", destroyMethod = "destoryMethod")
    public UserFactory userFactory() {
        UserFactory userFactory = new DefaultUserFactory();
        return userFactory;
    }

}

